package cn.globalph.housekeeper.ui.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.globalph.housekeeper.R

class OptionsDialog :BaseDialogFragment(){
    private lateinit var guideTv:TextView
    private lateinit var startServerTv:TextView
    private lateinit var endServerTv:TextView
    private lateinit var writeServerLogTv:TextView
    private lateinit var writeCheckCardTv:TextView

    private var listener:OptionClick? = null

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view  = LayoutInflater.from(context).inflate(R.layout.dialog_options,null)
        guideTv = view.findViewById(R.id.guide_tv)
        startServerTv = view.findViewById(R.id.start_server_tv)
        endServerTv = view.findViewById(R.id.end_server_tv)
        writeServerLogTv = view.findViewById(R.id.write_server_log_tv)
        writeCheckCardTv = view.findViewById(R.id.write_check_card_tv)

        guideTv.setOnClickListener {
            dismiss()
            listener?.onGuideClick()
        }

        startServerTv.setOnClickListener {
            dismiss()
            listener?.onStartServerClick()
        }

        endServerTv.setOnClickListener {
            dismiss()
            listener?.onCustomSign()
        }

        writeServerLogTv.setOnClickListener {
            dismiss()
            listener?.onWriteServerLog()
        }

        writeCheckCardTv.setOnClickListener {
            dismiss()
            listener?.onWriteCheckCard()
        }

        val dialog = Dialog(context!!,R.style.Dialog_NoTitle)
        dialog.setContentView(view)

        val layoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels - 160
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
        view.layoutParams = layoutParams
        dialog.window!!.setGravity(Gravity.CENTER)

        return dialog
    }


    fun setListener(listener:OptionClick){
        this.listener = listener
    }

    interface OptionClick{
        fun onGuideClick()
        fun onStartServerClick()
        fun onCustomSign()
        fun onWriteServerLog()
        fun onWriteCheckCard()
    }


    class SimpleAdapter: RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {
        private var list:List<String> = emptyList()
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_simple,parent,false))
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.bindData(list[position])
        }

        fun setData(list:List<String>){
            this.list = list
            notifyDataSetChanged()
        }

        class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
            private var tv:TextView? = null
            fun bindData(name:String){
                tv = itemView.findViewById(R.id.tv)
                tv?.text = name
            }
        }
    }
}